package com.dji.controller;

import com.dji.dao.DroneDao;
import com.dji.model.Drone;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 处理无人机相关请求的控制器
 */
public class DroneController extends HttpServlet {
    private DroneDao droneDao = new DroneDao();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        
        if (pathInfo == null || pathInfo.equals("/")) {
            // 获取所有无人机列表
            List<Drone> drones = droneDao.getAllDrones();
            request.setAttribute("drones", drones);
            request.getRequestDispatcher("/WEB-INF/views/drones.jsp").forward(request, response);
        } else if (pathInfo.startsWith("/view/")) {
            // 查看单个无人机详情
            String droneId = pathInfo.substring(6);
            Drone drone = droneDao.getDroneById(droneId);
            
            if (drone != null) {
                request.setAttribute("drone", drone);
                request.getRequestDispatcher("/WEB-INF/views/drone-detail.jsp").forward(request, response);
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "无人机未找到");
            }
        } else if (pathInfo.equals("/json")) {
            // 返回所有无人机的JSON数据
            List<Drone> drones = droneDao.getAllDrones();
            JSONArray jsonArray = new JSONArray();
            
            for (Drone drone : drones) {
                JSONObject jsonDrone = new JSONObject();
                jsonDrone.put("id", drone.getId());
                jsonDrone.put("model", drone.getModel());
                jsonDrone.put("batteryLevel", drone.getBatteryLevel());
                jsonDrone.put("latitude", drone.getLatitude());
                jsonDrone.put("longitude", drone.getLongitude());
                jsonDrone.put("altitude", drone.getAltitude());
                jsonDrone.put("speed", drone.getSpeed());
                jsonDrone.put("status", drone.getStatus());
                
                jsonArray.put(jsonDrone);
            }
            
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.print(jsonArray.toString());
            out.flush();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        
        if (pathInfo != null && pathInfo.equals("/control")) {
            // 处理无人机控制命令
            String droneId = request.getParameter("droneId");
            String action = request.getParameter("action");
            
            Drone drone = droneDao.getDroneById(droneId);
            if (drone != null) {
                switch (action) {
                    case "takeoff":
                        drone.setStatus("FLYING");
                        drone.setAltitude(10.0); // 默认起飞高度10米
                        break;
                    case "land":
                        drone.setStatus("LANDING");
                        drone.setAltitude(0.0);
                        break;
                    case "return":
                        drone.setStatus("RETURNING");
                        break;
                    case "hover":
                        // 保持当前高度和位置
                        break;
                    default:
                        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "未知的控制命令");
                        return;
                }
                
                droneDao.saveDrone(drone);
                
                // 返回成功响应
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                PrintWriter out = response.getWriter();
                out.print("{\"success\": true, \"message\": \"命令已发送\", \"status\": \"" + drone.getStatus() + "\"}\n");
                out.flush();
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "无人机未找到");
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}